package cn.pzhu.pserson.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.pzhu.pserson.dao.dao.MusicMapper;
import cn.pzhu.pserson.model.Music;
import cn.pzhu.pserson.util.SqlUtil;
import cn.pzhu.pserson.service.IMusicService;

@Service
public class MusicService implements IMusicService {
    @Resource
    private MusicMapper musicMapper;

    @Override
    public boolean save(Music music) {
        return musicMapper.insert(music) > 0;
    }

    @Override
    public boolean update(Music music) {
        return musicMapper.updateByPrimaryKeySelective(music) >= 0;
    }

    @Override
    public boolean delete(Integer id) {
        return musicMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean batchDelete(Integer[] primaryKeys) {
        return musicMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public Music findModel(Integer id) {
        return musicMapper.selectByPrimaryKey(id);
    }

    @Override
    public Music findModel(Integer id, String[] parameters) {
        return musicMapper.findModel(id, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(Integer id) {
        String[][] parameters = {{"id", "id"}, {"name", "name"}};
        return this.findMap(id, parameters);
    }

    public Map<String, Object> findMap(Integer id, String[][] parameters) {
        return musicMapper.findMap(id, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<Music> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<Music> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Music> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return musicMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"id", "id"}, {"name", "name"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"id", "id"}, {"name", "name"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return musicMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return musicMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

}
